﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BaMusaSoft.RPS.ViewModel;
using BaMusaSoft.RPS.DataModel;
using GalaSoft.MvvmLight.Command;

namespace BaMusaSoft.RPS.View
{
    /// <summary>
    /// Interaction logic for ClaimView.xaml
    /// </summary>
    public partial class ClaimView : UserControl
    {
        RelayCommand _addCustomerCommand;
        RelayCommand _addDetailsCommand;
        IUnitOfWork _unitOfWork;
        ClaimViewModel _vm;
        AddCustomerViewModel _addCustomerViewModel;
        public ClaimView()
        {
            InitializeComponent();
            
        }

        private void Grid_KeyDown(object sender, KeyEventArgs e)
        {
            var uie = e.OriginalSource as UIElement;
            if (e.Key == Key.Enter)
            {
                e.Handled = true;
                uie.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
            }
        }

        private void ViewLoaded(object sender, RoutedEventArgs e)
        {
            this.customerName.Focus();
            _unitOfWork = ((ClaimViewModel)DataContext).UnitOfWork;
            _vm = (ClaimViewModel)DataContext;
        }

        private void ClaimDetailSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            foreach (var item in e.AddedItems)
            {
                if (item is ClaimDetail)
                {
                    //((ClaimViewModel)this.DataContext).DeleteIsSelected = true;
                }
            }
            
        }

        public ICommand AddDetailsCommand
        {
            get
            {
                if (_addDetailsCommand == null)
                {
                    _addDetailsCommand = new RelayCommand(AddDetails, CanAddDetail);
                }
                return _addDetailsCommand;
            }
        }
        private void AddDetails()
        {
            var contractClaimedViewModel = new AddContractsClaimedViewModel(
                _unitOfWork, _vm.ClaimedCustomer);
            AddClaimDetailsWindow claimDetailsWindow = new AddClaimDetailsWindow(contractClaimedViewModel);
            if (claimDetailsWindow.ShowDialog() == true)
            {
                _vm.SetClaimDetails(contractClaimedViewModel.SelectedContracts);
            }
        }
        bool CanAddDetail()
        {
            if (_vm == null) return false;
            return _vm.ClaimedCustomer != null;
        }

        public ICommand AddCustomerCommand
        {
            get
            {
                if (_addCustomerCommand == null)
                {
                    _addCustomerCommand = new RelayCommand(AddCustomer);
                }
                return _addCustomerCommand;
            }
        }
        private void AddCustomer()
        {
            if(_addCustomerViewModel == null) _addCustomerViewModel = new AddCustomerViewModel(_unitOfWork);
           
            AddCustomerWindow customersWindow = new AddCustomerWindow(_addCustomerViewModel);
            if (customersWindow.ShowDialog() == true)
            {
                if (_addCustomerViewModel.SelectedCustomer != null)
                {
                    _vm.SetCustomer(_addCustomerViewModel.SelectedCustomer);
                }
            }
        }
    }
}
